package com.nwcl.ysx.controller;

import com.nwcl.basic.bo.CouponsReceiveBO;
import com.nwcl.basic.bo.UserBO;
import com.nwcl.basic.service.CouponsReceiveService;
import com.nwcl.basic.service.OrdersService;
import com.nwcl.basic.service.UserIntegralService;
import com.nwcl.basic.service.UserService;
import com.nwcl.common.constant.Constant;
import com.nwcl.common.result.Result;
import com.nwcl.common.utils.CheckUtil;
import com.nwcl.common.utils.OssUtil;
import com.nwcl.common.utils.SessionUtil;
import com.nwcl.ysx.request.UserInfoRequest;
import com.nwcl.ysx.service.SubscribeService;
import com.nwcl.ysx.vo.CouponsVO;
import com.nwcl.ysx.vo.MyHomeVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author liguiqin
 * @date 2023/5/25
 */
@RestController
@RequestMapping("/api/ysx/common")
public class ApiCommonController {
    @Autowired
    private SessionUtil sessionUtil;
    @Autowired
    private UserService userService;
    @Autowired
    private UserIntegralService userIntegralService;
    @Autowired
    private SubscribeService subscribeService;
    @Autowired
    private OrdersService ordersService;
    @Autowired
    private CouponsReceiveService couponsReceiveService;
    /*
     * @Description:查询我的信息接口
     * @Author liguiqin
     * @Date 2023/5/28 14:26
     * @param: param
     * @return com.nwcl.common.result.Result
     * @UpdateDate 2023/5/28 14:26
     **/
    @GetMapping("/myHome")
    public Result myHome(HttpServletRequest request) {
        Integer userId=sessionUtil.getUserId(request);
        UserBO userBO =userService.getById(userId);
        MyHomeVO vo =new MyHomeVO();
        vo.setHeadImgUrl(userBO.getHeadImgUrl());
        vo.setPhone(userBO.getPhone());
        vo.setIntegral(userIntegralService.getIntegralByUser(userId));
        vo.setUsername(userBO.getUsername());
        //设置预约次数
        int subscribeCount=subscribeService.countByUserId(userId);
        //设置订单数量
        int ordersCount=ordersService.countByUserId(userId);
        vo.setSubscribeCount(subscribeCount);
        vo.setOrdersCount(ordersCount);
        //我的优惠券
        List<CouponsReceiveBO> boList = couponsReceiveService.listByUserIdAndTime(userId, new Date());
        for (CouponsReceiveBO bo : boList) {
            CouponsVO couponsVO = new CouponsVO();
            couponsVO.setId(bo.getCouponsId());
            couponsVO.setName(Constant.Coupons.valMap.get(bo.getCouponsId()));
            couponsVO.setStatus(bo.getStatus());
            couponsVO.setStatusStr(Constant.CouponsReceiveStatus.valMap.get(bo.getStatus()));
            vo.getCouponsList().add(couponsVO);
        }
        return Result.success(vo);
    }

    /*
     * @Description: 编辑用户信息
     * @Author liguiqin
     * @Date 2023/5/30 13:39
     * @param:
     * @return com.nwcl.common.result.Result
     * @UpdateDate 2023/5/30 13:39
     **/
    @PostMapping(value = "/editUserInfo")
    public Result editUserInfo(@RequestBody UserInfoRequest param,HttpServletRequest request) {
        Integer userId = sessionUtil.getUserId(request);
        UserBO userBO = userService.getById(userId);
        if (StringUtils.isNotBlank(param.getUsername())) {
            userBO.setUsername(param.getUsername());
        }
        if (StringUtils.isNotBlank(param.getHeadImgUrl())) {
            userBO.setHeadImgUrl(param.getHeadImgUrl());
        }
        userService.updateUser(userBO);
        return Result.success();
    }

    /*
     * @Description: 上传图片视频
     * @Author liguiqin
     * @Date 2023/5/25 15:24
     * @param: file
     * @param: type
     * @return com.nwcl.common.result.Result
     * @UpdateDate 2023/5/25 15:24
     **/
    @PostMapping(value = "/uploadImgAndVideo")
    public Result uploadImg(MultipartFile file, Integer type) {
        String fileName = file.getOriginalFilename();
        String extName = fileName.substring(fileName.lastIndexOf(".") + 1);
        int MB=1024 * 1024;//1MB
        if (StringUtils.isBlank(extName)) {
            return Result.fail("资源信息不正确");
        }
        boolean matchSuffix = CheckUtil.checkImgAndVideoFileSuffix(file);
        if (!matchSuffix) {
            return Result.fail("图片视频格式错误，请上传JPEG/JPG/PNG/GIF/MP4/AVI/MOV/FLV/WMV/MKV/3GP/MPG");
        }
        //校验图片大小
        if (CheckUtil.checkImgFileSuffix(file) && file.getSize() > (1 *MB )) {
            throw new IllegalArgumentException("文件大小超过1MB的限制");
        }
        //校验视频大小
        if (CheckUtil.checkImgAndVideoFileSuffix(file) && file.getSize() > (100 * MB)) {
            throw new IllegalArgumentException("文件大小超过100MB的限制");
        }
        String folder = Constant.ResourcesPidType.pidTypeFolderMap.get(type);
        if (StringUtils.isBlank(folder)) {
            return Result.fail("资源类型错误");
        }
        String url = OssUtil.privateUploadFile(file, folder, extName);
        url = OssUtil.getOssUrl(url,null);
        url = StringUtils.substringBeforeLast(url, "?");
        Map<String, String> map = new HashMap<>();
        map.put("url", url);
        return Result.success(map);
    }
}
